﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ManagementSystem.DataSet1TableAdapters;
using System.Data;

namespace ManagementSystem
{
    /// <summary>
    /// Toimunud tundide vaatamine.
    /// </summary>
    public partial class VaataToimunudTunde : Window
    {
        User user;
        TundTableAdapter adapter;
        DataSet1 dataset;
        String aine;

        /// <summary>
        /// Konstruktor.
        /// </summary>
        /// <param name="user">Kasutaja, kellele kasutaja registreeritakse</param>
        public VaataToimunudTunde(User user, string aine)
        {
            InitializeComponent();
            this.user = user;
            FillTund();
            this.aine = aine;

             dataset = new DataSet1();
             adapter = new TundTableAdapter();
            adapter.Fill(dataset.Tund);
            
            dataset.Tund.TundRowChanged +=
        new DataSet1.TundRowChangeEventHandler(TundRowModified);
            dataset.Tund.TundRowDeleted +=
                new DataSet1.TundRowChangeEventHandler(TundRowModified);

            this.DataContext = dataset.Tund.DefaultView;

        }

        /// <summary>
        /// Kui muutub sisu mingis reas.
        /// </summary>
        /// <param name="sender">Sender objekt</param>
        /// <param name="e">Sündmuse parameetrid</param>
        void TundRowModified(object sender, DataSet1.TundRowChangeEvent e)
        {
            try
            {
                adapter.Update(dataset.Tund);
            }
            catch (Exception)
            {
                MessageBox.Show("Täida ära kõik väljad !");
            }
        }

        /// <summary>
        /// Täida tundide tabel infoga, mis erinevatest tabelitest kokku pannakse.
        /// </summary>
        private void FillTund()
        {
            try
            {
                DataClasses1DataContext kont = new DataClasses1DataContext();
                var dele = from dTund in kont.Tunds
                           where dTund.Tund_ID > 0
                           select dTund;
                foreach (var tun in dele)
                {
                    kont.Tunds.DeleteOnSubmit(tun);
                }
                kont.SubmitChanges();


                Tund les;
                var tunnid = from uTund in kont.Lessons 
                             from Tunniaine in kont.Subjects
                             where uTund.User_ID == user.User_ID && Tunniaine.Subject_ID == uTund.Subject_ID
                             select new {Tunniaine.Name, uTund.Topic, uTund.Descripton, uTund.Comment, uTund.Date };
                foreach (var tunnike in tunnid)
                {
                    les = new Tund();
                    les.Aine = tunnike.Name;
                    les.Teema = tunnike.Topic;
                    les.Kirjeldus = tunnike.Descripton;
                    les.Kommentaar = tunnike.Comment;
                    les.Kuupäev = tunnike.Date;
                    kont.Tunds.InsertOnSubmit(les);
                    kont.SubmitChanges();                    
                }
                kont.Dispose();
            }
            catch (Exception er)
            {
                MessageBox.Show("Tekkis viga andmete pärimisel"+ er.Message);
            }
        }



    }
}
